package io.renren.modules.sp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sp.dto.SpUserDTO;
import io.renren.modules.sp.excel.SpUserExcel;
import io.renren.modules.sp.service.SpTeamService;
import io.renren.modules.sp.service.SpUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* 用户
*
* @author whc 15953295779@126.com
* @since 3.0 2023-04-21
*/
@RestController
@RequestMapping("sp/spuser")
@Api(tags="用户")
public class SpUserController {
    @Autowired
    private SpUserService spUserService;

    @Autowired
    private SpTeamService spTeamService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    // @RequiresPermissions("sp:spuser:page")
    public Result<PageData<SpUserDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<SpUserDTO> page = spUserService.page(params);
        //查询代表队信息
        for (SpUserDTO spUserDTO : page.getList()) {
            Map<String, Object> teamInfo = spTeamService.queryTeamInfo(spUserDTO.getId());
            if(teamInfo == null){
                continue;
            }
            spUserDTO.setTeamName((String)teamInfo.get("team_name"));
            spUserDTO.setTeamCompany((String)teamInfo.get("company"));
            spUserDTO.setTeamLeader((String)teamInfo.get("leader"));
            spUserDTO.setTeamLeaderPhone((String)teamInfo.get("uphone"));
        }
        return new Result<PageData<SpUserDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    //@RequiresPermissions("sp:spuser:info")
    public Result<SpUserDTO> get(@PathVariable("id") Long id){
        SpUserDTO data = spUserService.get(id);
        Map<String, Object> teamInfo = spTeamService.queryTeamInfo(data.getId());
        if(teamInfo != null){
            data.setTeamName((String)teamInfo.get("team_name"));
            data.setTeamCompany((String)teamInfo.get("company"));
            data.setTeamLeader((String)teamInfo.get("leader"));
            data.setTeamLeaderPhone((String)teamInfo.get("phone"));
        }
        return new Result<SpUserDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    //@RequiresPermissions("sp:spuser:save")
    public Result save(@RequestBody SpUserDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setAccount(dto.getPhone());
        spUserService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    //@RequiresPermissions("sp:spuser:update")
    public Result update(@RequestBody SpUserDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        //判断手机号是否重复


        spUserService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    //@RequiresPermissions("sp:spuser:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        spUserService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    //@RequiresPermissions("sp:spuser:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SpUserDTO> list = spUserService.list(params);
        // roleId 0负责人 1领队 2老师 3选手
        for (SpUserDTO spUserDTO : list) {
            if("0".equals(spUserDTO.getRoleId())){
                spUserDTO.setRoleId("负责人");
            }else if("1".equals(spUserDTO.getRoleId())){
                spUserDTO.setRoleId("领队");
            }else if("2".equals(spUserDTO.getRoleId())){
                spUserDTO.setRoleId("老师");
            }else if("3".equals(spUserDTO.getRoleId())){
                spUserDTO.setRoleId("选手");
            }
        }
        ExcelUtils.exportExcelToTarget(response, null, "用户", list, SpUserExcel.class);
    }



}
